﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;
using System.Data.SQLite;
using System.IO;

namespace post.task
{
    public partial class add : Office2007Form
    {
        public add()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        public model.Task t = new model.Task();
        dll.dbhelp db = new dll.dbhelp();
        private void add_Load(object sender, EventArgs e)
        {
            textBox5.ReadOnly = true;
            textBox6.ReadOnly = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem itm = new ListViewItem(new string[] { textBox3.Text, "未导入值" });
            listView1.Items.Add(itm);
        }

        /// <summary>
        /// 加载任务
        /// </summary>
        public void load()
        {

        }

        private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (listView1.SelectedItems[0] != null)
                {
                    listView1.SelectedItems[0].SubItems[1].Text = open.FileName;
                }
            }
        }

        private void 查看ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要清空", "提示", MessageBoxButtons.OKCancel);
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                //执行清空
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除", "提示", MessageBoxButtons.OKCancel);
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                //执行删除
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (t.id != 0)
            {
                //执行添加的方法
                Thread th = new Thread(new ParameterizedThreadStart(xz));
                th.Start();
            }

        }
        //增加的方法
        private void xz(object obj)
        {
            try
            {
                //创建任务
                model.Task t = new model.Task();
                t.title = textBox1.Text;
                t.interval = int.Parse(textBox2.Text);
                t.noqj = int.Parse(textBox5.Text);
                t.timerqj = int.Parse(textBox6.Text);
                t.patid = comboBox1.Text;
                t.types = comboBox2.Text;
                t.oks = richTextBox1.Text;
                t.nos = richTextBox2.Text;
                string sql = "INSERT INTO ch_task (title,interval,noqj,timerqj,patid,types,oks,nos) VALUES (@title,@interval,@noqj,@timerqj,@patid,@types,@oks,@nos)";
                SQLiteCommand comm = new SQLiteCommand(sql);
                SQLiteParameter[] par = new SQLiteParameter[] {
                new SQLiteParameter("@title",t.title),
                 new SQLiteParameter("@interval",t.interval),
                  new SQLiteParameter("@noqj",t.noqj),
                   new SQLiteParameter("@timerqj",t.timerqj),
                   new SQLiteParameter("@patid",t.patid),
                   new SQLiteParameter("@types",t.types),
                   new SQLiteParameter("@oks",t.oks),
                   new SQLiteParameter("@nos",t.nos)
            };
                comm.Parameters.AddRange(par);
                Boolean boo = db.up(comm);
                if (!boo)
                {
                    MessageBox.Show("....创建失败");
                }
                else
                {
                    //返回任务id
                    sql = "select id from ch_task order by id desc limit 1";
                    comm = new SQLiteCommand(sql);
                    DataTable dt = db.select(comm);
                    int id = int.Parse(dt.Rows[0][0].ToString());
                    foreach (ListViewItem item in listView1.Items)
                    {
                        if (item != null)
                        {
                            sql = "INSERT INTO ch_parameter (name,tid) VALUES (@name,@tid)";
                            comm = new SQLiteCommand(sql);
                            par = new SQLiteParameter[]{
                        new SQLiteParameter("@name",item.SubItems[0].Text),
                        new SQLiteParameter("@tid",id),
                    };
                            comm.Parameters.AddRange(par);
                            db.up(comm);
                            sql = "select id from ch_parameter order by id desc limit 1";
                            comm = new SQLiteCommand(sql);
                            dt = db.select(comm);
                            int parid = int.Parse(dt.Rows[0][0].ToString());

                            StreamReader sr = new StreamReader(item.SubItems[1].Text, Encoding.Default);
                            String line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                sql = "INSERT INTO ch_zf (str,parid,tid) VALUES (@str,@parid,@tid)";
                                comm = new SQLiteCommand(sql);
                                par = new SQLiteParameter[] {
                            new SQLiteParameter("@str",line),
                            new SQLiteParameter("@parid",parid),
                            new SQLiteParameter("@tid",id)
                            };
                                comm.Parameters.AddRange(par);
                                db.up(comm);
                            }
                        }
                    }
                    MessageBox.Show("创建成功！");
                }
            }
            catch (Exception)
            {
               
            }
           
        }
    }
}
